<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/8/27 17:46
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Requests\Admin\Customer\Youzan;


use App\Http\Requests\ApiRequest;
use App\Services\Locale\Language;
use App\Services\Utils\ApiChecker;
use Carbon\Carbon;

/**
 * Class SyncOrdersRequest
 *
 *
 * @property string|null start_created
 * @property string|null end_created
 *
 * @package App\Http\Requests\Admin\Customer\Youzan
 */
class SyncOrdersRequest extends ApiRequest
{
    use ApiChecker;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'start_created' => 'nullable|required_with:end_created|date',
            'end_created'   => 'nullable|required_with:start_created|date',
        ];
    }

    public function checkTimeSpan()
    {
        if ($this->start_created && $this->end_created) {
            $start = new Carbon($this->start_created);
            $end = new Carbon($this->end_created);

            $this->checkParamError(
                $start->diffInMonths($end) <= 3,
                trans('admin.youzan_order_time_span_invalidate')
            );
        }
    }
}